# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        def reverse(start):
            last = start.next
            while last and last.next:
                nextNode = last.next
                last.next = nextNode.next
                nextNode.next = start.next
                start.next = nextNode

        slow = head
        fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        reverse(slow)
        left = head
        right = slow.next
        while right:
            if left.val != right.val:
                return False
            left = left.next
            right = right.next

        return True
